<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
  <div class="execution">
    <basic-container>
      <el-container>
        <el-aside width="200px">
          <avue-tree
            class="tree"
            :style="treeStyle"
            :option="treeOption"
            :data="treeData"
            :expandAll="true"
            @node-click="nodeClick"
          />
        </el-aside>
        <el-main>
          <avue-crud
            ref="crud"
            :page.sync="page"
            :data="tableData"
            :permission="permissionList"
            :table-loading="tableLoading"
            :option="tableOption"
            @on-load="getList"
            @search-change="searchChange"
            @refresh-change="refreshChange"
            @row-del="rowDel"
          >
            <template slot-scope="scope" slot="menu">
              <el-button type="text" size="small" @click="goDetail(scope.row)">详情</el-button>
              <el-button type="text" size="small" @click="goEdit(scope.row)">编辑</el-button>
            </template>
            <template slot="menuLeft">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="addMarket">新增市场</el-button>
              <market_import ref="marketImport" style="display: inline" />
              <el-button type="primary" size="small" round @click="moveMarket">迁移市场客户</el-button>
            </template>
          </avue-crud>
        </el-main>
      </el-container>
    </basic-container>
  </div>
</template>

<script>
import { delObj, fetchList } from "@/api/crm/marketmanage";
import { areaTree } from "@/api/crm/area";
import { tableOption } from "@/const/crud/crm/marketmanage";
import { mapGetters } from "vuex";
import market_import from "../../../components/crm/excel_import/market_import";

export default {
  name: "marketmanage",
  data() {
    return {
      treeOption: {
        nodeKey: "id",
        addBtn: false,
        menu: false,
        defaultExpandAll: true,
        props: {
          label: "areaName",
          value: "id"
        }
      },
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      treeData: [],
      tableHeight: "",
      treeStyle: {
        maxHeight: "",
        overflowX: scroll
      }
    };
  },
  components: {
    market_import
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.crm_marketmanage_add, false),
        delBtn: this.vaildData(this.permissions.crm_marketmanage_del, false),
        editBtn: this.vaildData(this.permissions.crm_marketmanage_edit, false)
      };
    }
  },
  created() {
    this.getHeight();
    this.getLeftList();
  },
  methods: {
    getHeight() {
      this.tableHeight = window.innerHeight - 367;
      this.tableOption.maxHeight = this.tableHeight + "px";
      this.treeStyle.maxHeight = this.tableHeight + 120 + "px";
    },
    nodeClick(data) {
      this.page.page = 1;
      this.getList(this.page, { areaSn: data.areaSn });
    },
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params
        )
      ).then(response => {
        this.tableData = response.data.data.records;
        this.page.total = response.data.data.total;
        this.tableLoading = false;
      });
    },
    getLeftList() {
      areaTree().then(res => {
        this.treeData = res.data.data;
      });
    },
    rowDel: function(row, index) {
      var _this = this;
      this.$confirm("是否确认删除市场名称为" + row.marketName, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delObj(row.id);
        })
        .then(data => {
          _this.tableData.splice(index, 1);
          _this.$message({
            showClose: true,
            message: "删除成功",
            type: "success"
          });
          this.getList(this.page);
        })
        .catch(function(err) {});
    },
    /**
     * 搜索回调
     */
    searchChange(form, done) {
      this.getList(this.page, form);
      done();
    },
    /**
     * 刷新回调
     */
    refreshChange(info) {
      this.getList(this.page, this.filterForm(info.searchForm));
    },
    addMarket() {
      this.$router.push({ path: "/basic/addMarket" });
    },
    goDetail(row) {
      let query = {
        res: {
          marketSn: row.marketSn
        }
      };
      this.$router.push({ path: "/basic/detailMarket", query: query });
    },
    goEdit(row) {
      let query = {
        res: {
          marketSn: row.marketSn
        }
      };
      console.log(query);
      
      this.$router.push({ path: "/basic/editMarket", query: query });
    },
    moveMarket() {
      this.$router.push({ path: "/basic/moveMarket" });
    }
  }
};
</script>

<style lang="scss" scoped>
.el-main {
  padding: 0 5px !important;
}
</style>
